package org.jsbd.boss.util;

import java.util.Random;

public class GetLoginCode {
    // 460026181416583
    // 862649020604428
    private static final String NETWORK_TYPE = "NONE";
    private static final String FLAG         = "@";
    private static final String SDK_VERSION  = "20120";    
    public static final int MG_CHANNEL_ZERO = 0;  //咪咕渠道零   

    public static char[]        charCode     = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();

    public static String getRandomStr(int len) {
        Random localRandom = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            int j = localRandom.nextInt(62);
            sb.append(String.valueOf(charCode[j]));
        }
        return sb.toString();
    }

    public static String getRandomInt(int digit) {
        Random localRandom = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < digit; i++) {
            sb.append(String.valueOf(localRandom.nextInt(10)));
        }
        return sb.toString();
    }

    
    public static String getMiguQipaiLoginCode(String imsi, String imei, String usr_tb_chid, String usr_tb_cid, String manufacturer, String model,String sdkVersion,String sdkSessionId,String randomCode, String os) {
        String loginParam = imsi + FLAG + randomCode+ FLAG + imei + FLAG + os + FLAG + usr_tb_chid + FLAG + usr_tb_cid + FLAG + manufacturer + FLAG
                + model + FLAG + "WIFI" + FLAG + sdkVersion + FLAG + sdkSessionId + FLAG + MG_CHANNEL_ZERO;
        return "BUB@T|" + encrypt(loginParam);
    }   
    
    /**
     * 
     * 150游戏基地登陆代码
     * @author <a href="mailto:zhaihuilin@zhexinit.com" >翟惠林</a>
     * @param imsi
     * @param imei
     * @param usr_tb_chid
     * @param usr_tb_cid
     * @param manufacturer
     * @param model
     * @param sdkVersion
     * @param sdkSessionId
     * @param randomCode
     * @param os
     * @return
     */
    public static String getOnlineLoginCode(String imsi, String imei, String usr_tb_chid, String usr_tb_cid, String manufacturer, String model,String sdkVersion,String sdkSessionId,String randomCode, String os) {
        String loginParam = imsi + FLAG + randomCode+ FLAG + imei + FLAG + os + FLAG + usr_tb_chid + FLAG + usr_tb_cid + FLAG + manufacturer + FLAG
                + model + FLAG + "WIFI" + FLAG + sdkVersion + FLAG + sdkSessionId + FLAG;
        return "BUB@T|" + encrypt(loginParam);
    }

    public static void main(String args[]){
        String test=getOnlineLoginCode("460022103156094", "865180029523929", "41402000", "607916026414", "BBK"  , "vivo Y18L");
        System.out.println(test);
    }
    
    public static String getOnlineLoginCode(String imsi, String imei, String usr_tb_chid, String usr_tb_cid, String manufacturer, String model) {
        String loginParam = imsi + FLAG + getRandomInt(4) + FLAG + imei + FLAG + "18" + FLAG + usr_tb_chid + FLAG + usr_tb_cid + FLAG + manufacturer + FLAG
                + model + FLAG + "WIFI" + FLAG + SDK_VERSION + FLAG + getRandomStr(12) + FLAG;
        return "BUB@T|" + encrypt(loginParam);
    }

    public static String getLoginCode(String imsi, String imei, String usr_tb_chid, String usr_tb_cid, String manufacturer, String model) {
        String loginParam = imsi + FLAG + getRandomInt(4) + FLAG + imei + FLAG + "18" + FLAG + usr_tb_chid + FLAG + usr_tb_cid + FLAG + manufacturer + FLAG
                + model + FLAG + NETWORK_TYPE + FLAG + SDK_VERSION + FLAG + getRandomStr(12) + FLAG;
        return "BUB@|" + encrypt(loginParam);
    }

    public static int    Ds = 8;
    private static int[] Dr = { 0, 5, 3, 4, 6, 7, 2, 1 };

    public static String encrypt(String str) {
        if ("".equals(str)) {
            return null;
        }
        int length = str.length();
        int rem = length % Ds;
        if (rem != 0) {
            char char1[] = new char[Ds - rem];
            for (int i = 0; i < char1.length; i++) {
                char1[i] = ' ';
            }
            StringBuilder sb = new StringBuilder(str);
            sb.append(new String(char1));
            str = sb.toString();
        }
        int sLen = str.length();
        char char2[] = new char[sLen];
        int m = 0;
        for (int i = 0; i < char2.length; i++) {
            char2[i] = str.charAt(Dr[i % Ds] + m * Ds);
            if ((i != 0) && ((i + 1) % Ds == 0))
                m++;
        }
        return reversalString(new String(char2));
    }
    private static String reversalString(String str) {

        StringBuffer s = new StringBuffer();
        if (str.isEmpty()) {
            return null;
        } else {
            StringBuffer sb = new StringBuffer(str);
            sb.reverse();
            for (int i = 0; i < sb.length(); i++) {
                char c = sb.charAt(i);
                if (isCharacters(c)) {
                } else {
                    c = (char) (c ^ 0x8);
                    if (isCharacters(c)) {
                        c = (char) (c ^ 0x8);
                    }
                }
                s.append(c);
            }
        }
        return s.toString();
    }
    private static boolean isCharacters(char c) {
        // 48 57 65 90 97 122
        return ((c < '0') || (c > '9')) && ((c < 'A') || (c > 'Z')) && ((c < 'a') || (c > 'z'));
    }
    /**
     * 离线
     * @param imsi
     * @param imei
     * @param usr_tb_chid
     * @param usr_tb_cid
     * @param manufacturer
     * @param model
     * @param sdkVersion
     * @param sdkSessionId
     * @param randomCode
     * @param os
     * @return
     */
    public static String getOnlineLoginCode130(String imsi, String imei, String usr_tb_chid, String usr_tb_cid, String manufacturer, String model,String sdkVersion,String sdkSessionId,String randomCode, String os) {
        String loginParam = imsi + FLAG + randomCode+ FLAG + imei + FLAG + os + FLAG + usr_tb_chid + FLAG + usr_tb_cid + FLAG + manufacturer + FLAG
                + model + FLAG + "WIFI" + FLAG + sdkVersion + FLAG + sdkSessionId + FLAG;
        return "BUB@|" + encrypt(loginParam);
    }
}
